'''
Gets the information about whether or not a Dell TechDirect user's account is active,
which is needed to use the Dispatch API.
''
'''
import PySimpleGUI as sg
from warranty_manager.claim.common import SUBMIT_COLOR, CANCEL_COLOR
from warranty_manager.gui.confirmation import get_confirmation
from warranty_manager.claim.dell.common import get_access_token, get_credentials_layout
from warranty_manager.claim.dell.dispatch_api import check_user
from warranty_manager.claim.dell.handlers import validate_common


sg.theme('Reddit')

layout = [
    *get_credentials_layout(sandbox=True),
    [
        sg.Button(
            'Check User',
            button_color=SUBMIT_COLOR,
            tooltip="Determines whether or not the specified user's account is active"
        ),
        sg.Cancel(button_color=CANCEL_COLOR)
    ]
]

window = sg.Window('Dell User Manager', layout)

while True:
    event, values = window.read()

    if event in (None, 'Cancel'):
        break
    elif event == 'Check User':
        try:
            username, password, sandbox = validate_common(values)
        except TypeError:
            continue

        if get_confirmation('get the information for the provided username and password'):
            token = get_access_token('Dispatch', sandbox=sandbox)

            if token:
                try:
                    full_name, inactive, locked = check_user(
                        username, password, token, sandbox=sandbox
                    )
                    sg.popup(
                        f'Name: {full_name}\nInactive: {inactive}\nLocked: {locked}',
                        title='Result'
                    )
                except AttributeError:
                    continue

    else:
        continue

window.close()
